<?php
define("NO_LOGIN",1);
require("global.php");
if($phprpg->input['submit']){
	if(strlen($phprpg->input['username']) < 3){
		$error[] = "Your username must be at least 3 characters";
	}
	if(strlen($phprpg->input['username']) > 25){
		$error[] = "Your username cannot be more than 25 characters";
	}
	if(!preg_match("/^[-_a-zA-Z0-9]+$/", $phprpg->input['username'])){
		$error[] = "Your username contains illegal characters";
	}
	if(strlen($phprpg->input['pass1']) < 3){
		$error[] = "Your password must be at least 3 characters";
	}
	if($phprpg->input['pass1'] != $phprpg->input['pass2']){
		$error[] = "The two passwords you entered do not match";
	}
	if(!preg_match("/^[-!#$%&\'*+\\.\/0-9=?A-Z^_`{|}~]+@([-0-9A-Z]+\.)+([0-9A-Z]){2,4}$/i",$phprpg->input['email'])){
		$error[] = "This is not a valid e-mail address";
	}
	if(!isset($error)){
		$checkUser = $db->select("players","*","username='".$phprpg->input['username']."'");
		if($db->recordcount($checkUser) == 1){
			$error[] = "This username is already taken";
		}
	}
	if(!isset($error)){
		$salt = md5(sha1(rand()));
		$saltedpass = sha1(md5($phprpg->input['pass1']).md5($salt));
		$insert['username'] = $phprpg->input['username'];
		$insert['password'] = $saltedpass;
		$insert['salt'] = $salt;
		$insert['email'] = $phprpg->input['email'];
		$insert['regdate'] = time();
		$db->insert("players",$insert);
		header("location: login.php");
	}else{
		$errors = $phprpg->inline_error($error);
	}
}
eval("\$register = \"".$templates->get("register")."\";");
$templates->output($register);
?>